package com.yuanduan.mplus.presenter;

import android.content.Context;
import android.graphics.PixelFormat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuanduan.mplus.R;

/**
 * Created by Administrator on 2016/9/23.
 */
public class FrameManager {
    private Context mContext;
    private FrameLayout frameLayout;
    private WindowManager.LayoutParams layoutParams;
    private ViewPager vp;
    private ImageView[] icons=new ImageView[5];
    private int[] imgSrc=new int[5];
    private String[] titles=new String[5];
    private String[] contents=new String[5];

    public FrameManager(Context context) {
        mContext=context;
        createView();
        setUp();
    }

    private void setUp() {
        icons[0]= (ImageView) frameLayout.findViewById(R.id.frame_dot1);
        icons[1]= (ImageView) frameLayout.findViewById(R.id.frame_dot2);
        icons[2]= (ImageView) frameLayout.findViewById(R.id.frame_dot3);
        icons[3]= (ImageView) frameLayout.findViewById(R.id.frame_dot4);
        icons[4]= (ImageView) frameLayout.findViewById(R.id.frame_dot5);
        imgSrc[0]=R.mipmap.frame_a;
        imgSrc[1]=R.mipmap.frame_b;
        imgSrc[2]=R.mipmap.frame_c;
        imgSrc[3]=R.mipmap.frame_d;
        imgSrc[4]=R.mipmap.frame_e;
        titles[0]="超速";
        titles[1]="超速";
        titles[2]="超速";
        titles[3]="超速";
        titles[4]="超速";
        contents[0]=mContext.getResources().getString(R.string.chaosu);
        contents[1]="haha";
        contents[2]="haha";
        contents[3]="haha";
        contents[4]="haha";
        vp.setAdapter(new MyAdapter());
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<icons.length;i++){
                    if(position==i){
                        icons[i].setImageResource(R.drawable.dot_blue);
                    }else{
                        icons[i].setImageResource(R.drawable.dot_white);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void show(){
        ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).addView(frameLayout,layoutParams);
    }

    private void createView(){
        frameLayout= (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_frame,null);
        layoutParams=new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.format= PixelFormat.TRANSLUCENT;
        layoutParams.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        frameLayout.setClickable(true);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).removeView(frameLayout);
            }
        });
        vp= (ViewPager) frameLayout.findViewById(R.id.frame_vp);
    }

    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imgSrc.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            LinearLayout ll= (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.frame_chaosu,null);
            ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            ((ImageView)ll.findViewById(R.id.frame_iv)).setImageResource(imgSrc[position]);
            ((TextView)ll.findViewById(R.id.frame_tv_title)).setText(titles[position]);
            ((TextView)ll.findViewById(R.id.frame_tv_content)).setText(contents[position]);
            ((TextView)ll.findViewById(R.id.frame_know)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).removeView(frameLayout);
                }
            });
            container.addView(ll);
            return ll;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }
    }

    public void destroy(){
        mContext=null;
        icons=null;
    }
}
